package com.zcy.hosp.controller;

import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcy.hosp.service.HospitalSetService;
import com.zcy.yygh.common.exception.GlobalEexceptionHandler;
import com.zcy.yygh.common.exception.YyghException;
import com.zcy.yygh.common.utils.MD5;
import com.zcy.yygh.common.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.jsqlparser.statement.select.Limit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@Api("医院设置")
@CrossOrigin
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;
    @GetMapping("/getAll")
    @ApiOperation("获取全部医院信息")
    public Result getAll(){
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }
    @ApiOperation("逻辑删除")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable("id") Long id){
        boolean b = hospitalSetService.removeById(id);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    @PostMapping("/pageQuerVo/{current}/{limit}")
    public Result pageQuerVo(@PathVariable("current") Long current,
                             @PathVariable("limit") Long Limit,
                             @RequestBody(required = false)HospitalSetQueryVo hospitalSetQueryVo){
        Page<HospitalSet> page = new Page<>(current,Limit);
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hosname = "";
        String hoscode = "";
        if (hospitalSetQueryVo != null){
             hosname = hospitalSetQueryVo.getHosname();
             hoscode = hospitalSetQueryVo.getHoscode();
        }
        if (!StringUtils.isEmpty(hoscode)){
            wrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
        }
        if (!StringUtils.isEmpty(hosname)){
            wrapper.eq("hosname",hospitalSetQueryVo.getHosname());
        }
        Page<HospitalSet> page1 = hospitalSetService.page(page,wrapper);
        return Result.ok(page1);
    }
    @PostMapping("/saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        hospitalSet.setStatus(1);
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        boolean save = hospitalSetService.save(hospitalSet);
        if (save){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    @PostMapping("/selectById/{id}")
    public Result selectById(@PathVariable("id") Long id){
        HospitalSet byId = hospitalSetService.getById(id);
        return Result.ok(byId);
    }
    @PostMapping("/update")
    public Result update(@RequestBody HospitalSet hospitalSet){
        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b){
           return Result.ok();
        }else {
            return Result.fail();
        }
    }
    @PostMapping("/deletas")
    public Result deletas(@RequestBody List<Long> list){
        boolean b = hospitalSetService.removeByIds(list);
        return Result.ok();
    }
    @PostMapping("/lockHospital/{id}/{status}")
    public Result lockHospital(@PathVariable("id") Long id,
                               @PathVariable("status") Integer status){
        HospitalSet byId = hospitalSetService.getById(id);
        byId.setStatus(status);
        hospitalSetService.updateById(byId);
        return Result.ok();
    }
    @GetMapping("/sendKey/{id}")
    public Result sendKey(@PathVariable("id") Integer id){
        HospitalSet byId = hospitalSetService.getById(id);
        String signKey = byId.getSignKey();
        String hoscode = byId.getHoscode();
        return Result.ok();
    }

}
